﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;

namespace ESOL.Business.Proj
{
    public class ProjMBOMMaterialBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objBomProductBomMaterialEntity">Entity</param>
        /// <returns>Entity</returns>
        public static BomProductBomMaterialEntity GetBomProductBomMaterial(BomProductBomMaterialEntity objBomProductBomMaterialEntity)
        {
            return DataAccess.SelectSingle<BomProductBomMaterialEntity>(objBomProductBomMaterialEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objBomProductBomMaterialEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertBomProductBomMaterial(BomProductBomMaterialEntity objBomProductBomMaterialEntity)
        {
            return DataAccess.Insert(objBomProductBomMaterialEntity);
        }

        public static bool InsertBomProductBomMaterial(List<BomProductBomMaterialEntity> objBomProductBomMaterialEntityList)
        {
            return DataAccess.Insert(objBomProductBomMaterialEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objBomProductBomMaterialEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateBomProductBomMaterial(BomProductBomMaterialEntity objBomProductBomMaterialEntity)
        {
            return DataAccess.Update(objBomProductBomMaterialEntity);
        }

        public static bool UpdateBomProductBomMaterial(List<BomProductBomMaterialEntity> objBomProductBomMaterialEntityList)
        {
            return DataAccess.Update<BomProductBomMaterialEntity>(objBomProductBomMaterialEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objBomProductBomMaterialEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteBomProductBomMaterial(BomProductBomMaterialEntity objBomProductBomMaterialEntity)
        {
            return DataAccess.Delete(objBomProductBomMaterialEntity);
        }

        public static bool DeleteBomProductBomMaterial(List<BomProductBomMaterialEntity> objBomProductBomMaterialEntityList)
        {
            return DataAccess.Delete<BomProductBomMaterialEntity>(objBomProductBomMaterialEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetBomProductBomMaterialAll()
        {
            return DataAccess.SelectAllDataSet<BomProductBomMaterialEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<BomProductBomMaterialEntity> GetBomProductBomMaterialAllList()
        {
            return DataAccess.SelectAll<BomProductBomMaterialEntity>();
        }
        #endregion

        #region Functions
        public static List<BomProductBomMaterialEntity> GetAllByProductID(string sProductID)
        {
            string sql = @"select bpbm.*  from BOM_PRODUCT_BOM_MATERIAL bpbm
                where bpbm.PRODUCT_ID='" + sProductID + "' ";
            return DataAccess.Select<BomProductBomMaterialEntity>(sql);
        }

        public static DataTable GetDtAllByProductID(string sProductID)
        {
            string sql = @"select bpbm.*,mp.PRODUCT_NAME as MATERIAL_NAME,mp.PRODUCT_CODE as MATERIAL_CODE  
                from BOM_PRODUCT_BOM_MATERIAL bpbm
                left join MST_PRODUCT mp on bpbm.MATERIAL_ID=mp.PRODUCT_ID
                where bpbm.PRODUCT_ID='" + sProductID + "' ";
            DataSet ds = new DataSet();
            ds = DataAccess.SelectDataSet(sql);
            return ds.Tables[0];
        }

        /// <summary>
        /// 根据模糊条件查询物料对应原料表数据，如多条则选取首条
        /// </summary>
        /// <param name="sCondition"></param>
        /// <returns></returns>
        public static DataTable GetDtAllByFuzzyCondition(string sCondition)
        {
            string sql = @"select bpbm.*,mp.PRODUCT_NAME as MATERIAL_NAME,mp.PRODUCT_CODE as MATERIAL_CODE  
                from dbo.BOM_PRODUCT_BOM_MATERIAL bpbm
                left join MST_PRODUCT mp on bpbm.MATERIAL_ID=mp.PRODUCT_ID
                where bpbm.PRODUCT_ID in (
                select top 1 mp.PRODUCT_ID from MST_PRODUCT mp where 1=1 ";
            if (string.IsNullOrEmpty(sCondition) == false)
            {
                sql += sCondition;
            }
            sql += ") ";
            return DataAccess.SelectDataSet(sql).Tables[0];
        }

        public static bool Save(MstProductEntity m, DataTable dt)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    List<BomProductBomMaterialEntity> list = GetAllByProductID(m.ProductId);
                    if (list != null && list.Count > 0)
                    {
                        DataAccess.Delete<BomProductBomMaterialEntity>(list, broker);
                    }
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        list = new List<BomProductBomMaterialEntity>();
                        foreach (DataRow dr in dt.Rows)
                        {
                            BomProductBomMaterialEntity entity = new BomProductBomMaterialEntity(dr);
                            list.Add(entity);
                        }
                        DataAccess.Insert<BomProductBomMaterialEntity>(list, broker);
                    }
                    else
                    {
                        return false;
                    }
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }
        #endregion

    }
}
